﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using TicketM.ReportService;

namespace TicketM
{
    public partial class _Default : Page
    {
        private string ProcessConfigPath;
        private string EscalationsConfigPath;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Process.GetCurrentProcess().ProcessName.ToLower().Contains("iisexpress"))
            {
                ProcessConfigPath = "/App_Data/Projects.xml";
                EscalationsConfigPath = "/App_Data/AreaNoDaysEmails.xml";
            }
            else
            {
                ProcessConfigPath = "/TicketM/App_Data/Projects.xml";
                EscalationsConfigPath = "/TicketM/App_Data/AreaNoDaysEmails.xml";
            }
            if (!Page.IsPostBack)
            {
            
                GetProjectsTicketedNonTicketed();
            }
            
        }

        private void GetProjectsTicketedNonTicketed()
        {
            Models.Tickets ticketing = new Models.Tickets();
            ArrayList projects = null;
            try
            {
                projects = ticketing.GetProjects();
            }
            catch (Exception ex)
            {
                Logger.CreateLogEntry("!!ERROR in Default.aspx.cs while getting the project list: " + ex.Message);
                Response.Redirect("CommonErrorPage.aspx");
            }

            XmlDocument doc = new XmlDocument();
            
            doc.Load(Server.MapPath(ProcessConfigPath));

            foreach (string project in projects)
            {
                if (doc.DocumentElement.SelectSingleNode("/projects/project[@name='" + project + "']") == null)
                {
                    this.NonTicketed.Items.Add(project);
                }
                else
                {
                    this.Ticketed.Items.Add(project);
                }
            }
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            Models.Tickets ticketing = new Models.Tickets();

            try
            {
                if (ticketing.AddTicketWIT2Project(Session["project"].ToString(),ProcessConfigPath,EscalationsConfigPath))
                {
                    NonTicketed.Items.Clear();
                    Ticketed.Items.Clear();
                    GetProjectsTicketedNonTicketed();
                    Save.Enabled = false;
                    addButton.Enabled = true;
                    //upload reports to the project
                    ticketing.UploadReports(Session["project"].ToString());
                    Logger.CreateLogEntry("reports uploadted");
                    Logger.CreateLogEntry("** INFO Ticketing Support added to project " + Session["project"].ToString() + " at " + DateTime.Now.ToString());
                }
            }
            catch (Exception ex)
            {
                Logger.CreateLogEntry("!!ERROR in Default.aspx.cs while Adding Ticketing support to project: " + ex.Message);
            }
        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            this.Ticketed.Items.Add(this.NonTicketed.SelectedItem);
            Session.Add("project", this.NonTicketed.SelectedItem.Text);
            this.NonTicketed.Items.Remove(this.NonTicketed.SelectedItem);
            addButton.Enabled = false;
            Save.Enabled = true;
        }
    }
}